/*
 * Ext GWT - Ext for GWT
 * Copyright(c) 2007-2009, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */
package web.mail.client;

import java.util.List;

import web.mail.client.model.MailItem;
import web.mail.client.ui.MailFolderPanel;
import web.mail.client.ui.MailListPanel;
import web.mail.client.ui.MailItemPanel;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;

import web.app.client.model.Folder;

public class MailView extends View {


  public LayoutContainer centerPanel;
  public LayoutContainer viewport;
  public MailFolderPanel mailFolderPanel;
  private MailListPanel mailListPanel;
  private MailItemPanel mailItemPanel;

  public MailView(Controller controller) {
    super(controller);

    viewport = new LayoutContainer();
    viewport.setLayout(new BorderLayout());

    centerPanel = new LayoutContainer();
    BorderLayout layout = new BorderLayout();
    layout.setEnableState(false);
    centerPanel.setLayout(layout);

    mailListPanel = new MailListPanel();
    centerPanel.add(mailListPanel, new BorderLayoutData(LayoutRegion.CENTER));

    mailItemPanel = new MailItemPanel();
    BorderLayoutData southData = new BorderLayoutData(LayoutRegion.SOUTH, .5f, 200, 1000);
    southData.setSplit(true);
    southData.setMargins(new Margins(5, 0, 0, 0));
    centerPanel.add(mailItemPanel, southData);

    mailFolderPanel = new MailFolderPanel();

    ContentPanel sideBar = new ContentPanel();
    sideBar.setHeading("Mail");
    sideBar.add(mailFolderPanel);
    sideBar.setLayout(new FitLayout());
    sideBar.setLayoutOnChange(true);

    BorderLayoutData region = new BorderLayoutData(LayoutRegion.WEST, 200, 150, 350);
    region.setMargins(new Margins(0, 5, 0, 0));
    viewport.add(sideBar,region);
    region = new BorderLayoutData(LayoutRegion.CENTER);
    viewport.add(centerPanel, region);

    initEvents();
    
  }

  protected void initEvents(){

    mailFolderPanel.getSelectionModel().addSelectionChangedListener(
        new SelectionChangedListener<Folder>() {
          @Override
          public void selectionChanged(SelectionChangedEvent<Folder> se) {    
            Dispatcher.forwardEvent(
    			new AppEvent( MailEvents.FolderSelected, se.getSelectedItem() )
    		);
          }
        }
    );
  }




    @SuppressWarnings("unchecked")
	@Override protected void handleEvent(AppEvent e) {
        EventType et = e.getType();
        if( et == MailEvents.FoldersUpdated ){
        	mailFolderPanel.load( (Folder) e.getData() );
        }
        else if( et == MailEvents.FolderChanged ){
        	mailListPanel.getStore().removeAll();
            mailListPanel.getStore().add( (List<MailItem>) e.getData() );
        }

        /*
if (event.getType() == MailEvents.ViewMailItems) {
      Folder f = (Folder) event.getData("folder");
      mailListPanel.setHeading(f.getName());

      ListStore<MailItem> store = mailListPanel.getStore();
      store.removeAll();

      store.add(event.<List<MailItem>>getData());

      //wrapper.layout();

      List<MailItem> items = event.getData();
      if (items.size() > 0) {
        mailListPanel.getGrid().getSelectionModel().select(((MailItem) items.get(0)), false);
      } else {
        mailItemPanel.showItem(null);
      }
      return;
    }

    if (event.getType() == MailEvents.ViewMailItem) {
      MailItem item = event.getData();
      mailItemPanel.showItem(item);
    }
         *
         */
    }

}
